<?php
namespace app\admin\controller;
use think\Controller;
use think\Validate;
use think\Db;
use think\Session;
use think\File;
class User extends Basic
{  

	 public function userdj()
    {
     $user= Db::name('memberdj')->paginate(10);
	 $this->assign('user',$user); 
     return $this->fetch();
    }
    public function userdjxg()
    {//修改
		 $post=input("post.");
			       
		if($post){
		  $id=$post['id'];unset($post['id']);
		   $fin=Db::name('memberdj')->where('id',$id)->find();
           if(!$fin){ $this->success('该等级不存在');}
		   Db::name('memberdj')->where('id',$id)->update($post);
		   $this->success('操作成功！');
		}else{
     $id=input('param.id');
    $user= Db::name('memberdj')->where('id',$id)->find();
	
	 $this->assign('userf',$user); 
     return $this->fetch();
	   }
    }

	 public function userdjadd()
    {//添加
		 $post=input("post.");
			       
		if($post){

		   $fin=Db::name('memberdj')->where(array('name'=>$post['name']))->find();
		   //dump($fin);die;
           if($fin){ $this->success('该等级已存在');}
		   $post['addtime']=date("Y-m-d H:i:s");
		   $insert=Db::name('memberdj')->insert($post);
		   $this->success('操作成功！');
		}else{
     $id=input('param.id');
  
	
	 $this->assign('id',$id); 
     return $this->fetch();
	   }
    }

	public function userdjdel()
    {//删除
		 $post=input("param.");
			       
		if($post){
		  $id=$post['id'];unset($post['id']);
		  $fin=Db::name('memberdj')->where('id',$id)->find();
           if(!$fin){ $this->success('该等级不存在');}
		   Db::name('memberdj')->where('id',$id)->delete();
		   $this->success('操作成功！');
		}
    }


    public function userlist()
    {

    $post=input("post.");
	$name='';		       
		if($post and $post['name']!=''){
		 $user= Db::name('member')->where('name',$post['name'])->order('id desc')->paginate(10);$name=$post['name'];
		}else{
    $user= Db::name('member')->order('id desc')->paginate(10);}
	 $this->assign('user',$user); 
	  $this->assign('name',$name); 
     return $this->fetch();
    }
	public function userlistxx()
    {
		$id=input('param.id');
    $user= Db::name('member')->where('id',$id)->find();
	
	 $this->assign('userf',$user); 
     return $this->fetch();
    }
	public function upuser()
    {

		$post=input("post.");
			       
		if($post){
			
	      
           $up=upload('image');
		   if(!empty($up['error'])){
		   $this->error($up['error']);
		   
		   }
		   if($up){
		    $post['txpic']=$up;
		   }else{
		   unset($post['txpic']);
		   }
		  
		   $id=$post['id'];
           $user= Db::name('member')->where('id',$post['id'])->find();
		   if($post['pwd']==$user['pwd']){unset($post['pwd']);}
           else $post['pwd']=md5($post['pwd']);
           if($post['paypwd']==$user['paypwd']){unset($post['paypwd']);}
           else $post['paypwd']=md5($post['paypwd']);
           unset($post['id']);
           Db::name('member')->where('id',$id)->update($post);
		   $this->success('操作成功！');
		
		}else{
		$id=input('param.id');
    $user= Db::name('member')->where('id',$id)->find();
	
	 $this->assign('userf',$user); 
     return $this->fetch();
	   }
    }
   public function  adduser(){
   
   
     $post=input("post.");
			       
		if($post){
			
	      
         
		  
		   $name=$post['name'];
           $user= Db::name('member')->where('name',$post['name'])->find();
		   if($user){$this->error('用户名已注册！');}
		  
           $mobile= Db::name('member')->where('mobile',$post['mobile'])->find();
           if($mobile){$this->error('手机号已注册！');}
		   if($post['pwd']=='')$this->error('密码不能为空！');
          if($post['paypwd']=='')$this->error('交易密码不能为空！');
		   if($post['mobile']=='')$this->error('手机号不能为空！');
		   if($post['name']=='')$this->error('用户名不能为空！');
		   $post['pwd']=md5($post['pwd']);
           $post['paypwd']=md5($post['paypwd']);
		   $post['regtime']=date("Y-m-d H:i:s");
           $insert=Db::name('member')->insert($post);
		   if($insert)$this->success('添加成功！');
		   else $this->error('添加失败！');
		
		}else{
		$id=input('param.id');
    $user= Db::name('member')->where('id',$id)->find();
	
	 $this->assign('userf',$user); 
     return $this->fetch();
	   }
   }
  public function usermoneyxg(){//操作金额
   
   
     $post=input("post.");
			       
		if($post){
         $money=$post['money'];
		 if($money==''){exit(json_encode(array('status'=>'0','msg'=>'金额不能为空')));}
         $user=Db::name('member')->where('id',$post['id'])->find();
		 if(!$user){exit(json_encode(array('status'=>'0','msg'=>'账户不存在')));}
		 if($post['type']=='+'){
         $config= Db::name('config')->find();
		  $about='系统充值';
		  $acti='+';
		  $pay= Db::name('pay')->where('state','1')->find();
		  $date['userid']= $user['id'];
         if($pay)$date['paytype']=$pay['id'];
		 $date['money']=$money;
		 $date['number']= $user['id']."_".time();
		  $date['hksm']='';
		  $date['state']='1';
		  $date['addtime']=date("Y-m-d H:i:s");
		  $insert=Db::name('paylist')->insert($date);
             $config= Db::name('config')->find();

		    rechargefd($money/$config['czbs'],$post['id'],$config['zcfdbl'],0,$user['name']);//上级返利
             $con='您的充值余额'.$money.'，已到账';
             addnews('系统充值',$con,$post['id']);
		 }else{
          if($user['amount']<$money){exit(json_encode(array('status'=>'0','msg'=>'账户余额不足')));}
		   $about='管理员扣除金额';
		   $acti='-';
		 }
		 handlelog($post['id'],$money,$acti,1,$about);
		 exit(json_encode(array('status'=>'1','msg'=>'操作成功')));
         
		
		
		}
   
   }
	public function deleuser()
    {//删除会员

		$id=input('param.id');
			       
		if($id){
			
	      
          
		   
           $user= Db::name('member')->where('id',$id)->find();
		   if($user){
			   Db::name('member')->where('id',$id)->delete();
		    $this->success('操作成功！');
		   }else{
		    $this->success('该会员不存在');
		   }
		  
		  
		
		}
    }

	public function loginqd()
    {
		$id=input('param.id');
    $user= Db::name('member')->where('id',$id)->find();
	
	Session::set('userid',$user['id']);
	 header('Location: /index/');die;
   
    }


//推送信息

	public function xxlist(){
	  $list=Db::name('newsabount')->order('id desc')->paginate(10);
       $this->assign("log",$list);
	     return $this->fetch();
		 
		
	
	}
  public function upxx(){
	  $post=input("post.");
			       
		if($post){
		  if($post['title']==''){$this->error('标题不能为空');}
		  if($post['content']==''){$this->error('内容不能为空');}
		  $id=$post['id'];
		   $insert=Db::name('newsabount')->where("id",$id)->update($post);
		   if($insert)$this->success('修改成功！');
		   else $this->error('修改失败！');

		
		}else{
          $id=input('param.id');
           $list=Db::name('newsabount')->where("id",$id)->order('id desc')->find();
           $this->assign("log",$list);
	     return $this->fetch();
		 
		 }
		 
		
	
	}
  
  public function xxdele()
    {//删除
		 $post=input("param.");
			       
		if($post){
		  $id=$post['id'];unset($post['id']);
		  $fin=Db::name('newsabount')->where('id',$id)->find();
           if(!$fin){ $this->success('该公告不存在！');}
		   Db::name('newsabount')->where('id',$id)->delete();
		   $this->success('操作成功！');
		}
    }
	//推送信息

	public function tsxx(){
	 $post=input("post.");
			       
		if($post){
		  if($post['title']==''){$this->error('标题不能为空');}
		  if($post['content']==''){$this->error('内容不能为空');}
		   $post['addtime']=date("Y-m-d H:i:s");
		   $insert=Db::name('newsabount')->insert($post);
		   if($insert)$this->success('添加成功！');
		   else $this->error('添加失败！');

		
		}else{
	     return $this->fetch();
		 
		 }
	
	}

	//会员日志

	public function moneylog(){
		 $id='';
	 if(input('param.')){$id=input('param.id');}
	if(!$id){ $log=Db::name('moneylog')->order('id desc')->paginate(10);}
	else{ 
       if($id){
       $user= Db::name('member')->where("name",$id)->find();
       if($user) $array['userid']=$user['id'];
         else $array['userid']=$id;
      }
      
      $log=Db::name('moneylog')->order('id desc')->where($array)->paginate(10);}

	$this->assign("idf",$id);
      $this->assign("log",$log);
	return $this->fetch();
	
	}
	//充值
   public function czlog(){
	 
  $id='';
  
	 if(input('param.')){$id=input('param.id');}
	if(!$id and input('param.state')!=='0' and input('param.state')!=='1' and input('param.state')!=='2'){$log=Db::name('paylist')->order('id desc')->paginate(10);}
	else{
		if($id)$array['userid']=$id;
      
      if($id){
       $user= Db::name('member')->where("name",$id)->find();
       if($user) $array['userid']=$user['id'];
      }
      
		if(input('param.state')>=0 and !empty(input('param.state'))){
		$array['state']=input('param.state');
		}
      if(input('param.state')==='0' ){
		$array['state']=input('param.state');
		} 
		
		$log=Db::name('paylist')->order('id desc')->where($array)->paginate(10);
	
	}

	 $pay=Db::name('pay')->select();
	   foreach($pay as $va){
	     $arr[$va['id']]=$va['zwname'];
	   }
	   $date['pay']=$arr;
	 $this->assign($date);
     $idf=Db::name('paylist')->max('id');
     Db::name('paylist')->where("id",$idf)->update(array("yid"=>'1'));
	  $this->assign("idf",$id);
      $this->assign("log",$log);
	return $this->fetch();
	
	
	}

	public function czlogcl(){//处理充值
	 
  $arr=input('param.');
  if(empty($arr['id'])){$this->error("未知错误");}
	$id=input('param.id');
	 $log=Db::name('paylist')->where('id',$id)->find();
	 if($log['state']!='0'){$this->error("该记录已处理");}
	 else{
		 $date['state']=input('param.state');
		 $up=Db::name('paylist')->where('id',$id)->update($date);
         $config= Db::name('config')->find();
		    if($date['state']=='1'){//成功加金额
			   $money=$log['money']*$config['czbs'];
			  $kk=1;
			  if($kk){

                  rechargefd($money/$config['czbs'],$log['userid'],$config['zcfdbl'], 0,membername($log['userid']));//上级返利
                  handlelog($log['userid'],$money,'+',1,'系统充值');
                  $con='您的充值余额'.$money.'，已到账';
                  addnews('系统充值',$con,$log['userid']);
			   $this->success('操作成功');
			  }else{
			  $this->error('操作失败');
			    }
			 }elseif($date['state']=='2'){//
			 
			  if($up)$this->success('操作成功');
			  else $this->error('操作失败');
			   
			 }
		 }

	
      $this->assign("log",$log);
	
	
	}
	//提现
	public function txlog(){
	 
  $id='';
  $name='';
	 if(!empty(input('param.id'))){$id=input('param.id');} 
      
      
	if( !$id and input('param.state')!=='0' and input('param.state')!=='1' and input('param.state')!=='2'){$log=Db::name('txlist')->order('id desc')->paginate(10);}
	else{
   
		if($id)$array['userid']=$id;
     
      if($id){
       $user= Db::name('member')->where("name",$id)->find();
       if($user) $array['userid']=$user['id'];
      }
     
       
		if(input('param.state')>=0 and !empty(input('param.state'))){
		$array['state']=input('param.state');
		}
      if(input('param.state')==='0' ){
		$array['state']=input('param.state');
		}   
   
		
		$log=Db::name('txlist')->order('id desc')->where($array)->paginate(10);
      
	
	}$idf=Db::name('txlist')->max('id');
     Db::name('txlist')->where("id",$idf)->update(array("yid"=>'1'));

	  $this->assign("idf",$id);
      $this->assign("log",$log);
	return $this->fetch();
	
	
	}

	public function txlogcl(){//处理提现
	 
  $arr=input('param.');
  if(empty($arr['id'])){$this->error("未知错误");}
	$id=input('param.id');
	 $log=Db::name('txlist')->where('id',$id)->find();
	 if($log['state']!='0'){$this->error("该记录已处理");}
	 else{ 
		 
		 $date['state']=input('param.state');
		 $up=Db::name('txlist')->where('id',$id)->update($date);
		    if($date['state']=='1'){
			  
			  if($up)$this->success('操作成功');
			  else $this->error('操作失败');
			 }elseif($date['state']=='2'){//失败金额返回
			  $money=$log['money'];
			  $kk= 1;
			  if($kk){
                handlelog($log['userid'],$money,'+',9,'提现失败,金额返还！');
			   $this->success('操作成功');
			  }else{
			  $this->error('操作失败');
			    }
			   
			 }
		 }

	
      $this->assign("log",$log);
	
	
	}
	
}
